package com.peg.rules;

import com.peg.dataobjects.Context;
import com.peg.exceptions.ParserException;

/**
 * @author sad
 */
public class RuleCallRule extends Rule {

    private String ruleToCall;

    public RuleCallRule(String ruleToCall) {
        this.ruleToCall = ruleToCall;
    }

    @Override
    public boolean execute(Context context) {
        Rule rule=context.getRule(ruleToCall);
        if(rule==null){
            throw new ParserException("Cannot find rule "+ruleToCall, context.getCurrentStreamIndex());
        }

        return executeRule(rule, context);
    }

    public String getRuleToCall() {
        return ruleToCall;
    }

    @Override
    public String toString() {
        return String.format("Call rule [%s]", ruleToCall);
    }
}